package io.shallwe.history;

import com.beust.jcommander.internal.Sets;
import com.google.common.collect.Lists;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheKit;
import io.shallwe.common.model.Toc;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author : 王小伟
 * @date : 2019-08-08 20:59:56
 */
public class LogTocService {

    private Toc dao = new Toc().dao();

    /**
     *列出所有参数配置信息
     * @return
     */
    public List<Toc> listAllToc(){
        List<Toc> tocList = CacheKit.get("toc", "tocList");
        if (tocList == null) {
            tocList = dao.template("log.listAllToc").find();
            CacheKit.put("toc", "tocList", tocList);
        }
        return tocList;
    }
    /**
     *列出所有参数配置信息
     * @return
     */
    public List<Toc> listAllTocForTest(){
        List<Toc> tocList = CacheKit.get("toc", "tocList");
        if (tocList == null) {
            tocList = dao.template("log.listAllTocForTest").find();
            CacheKit.put("toc", "tocList", tocList);
        }
        return tocList;
    }

    /**
     * 列出所有工位参数配置信息
     * @return
     */
    public List<Toc> listAllLocationParameterConfig(){
        List<Toc> locationParameterConfig = CacheKit.get("toc", "listAllLocationParameterConfig");
        if(locationParameterConfig==null) {
            List<Toc> allToc = this.listAllToc();
            locationParameterConfig = Lists.newArrayList();
            for (Toc toc : allToc) {
                if (toc.getParameterType().intValue() == 1) {
                    locationParameterConfig.add(toc);
                }
            }
            CacheKit.put("toc", "listAllLocationParameterConfig",locationParameterConfig);
        }
        return locationParameterConfig;
    }

    /**
     * 列出所有工位参数配置信息
     * @return
     */
    public  Map<String, List<Toc>> getLocationParameterConfigMap(){
        List<Toc> list = this.listAllLocationParameterConfig();
        Map<String, List<Toc>> map = list.stream().collect(Collectors.groupingBy(config -> config.getLocationCode()));
        return  map;
    }

    /**
     * 查询所有需要记录参数的工位
     * @return
     */
    public Set<String> getParameterLocation(){
        Set<String> parameterLocation = CacheKit.get("toc", "parameterLocation");
        if(parameterLocation==null) {
            parameterLocation= Sets.newHashSet();
            List<Toc> allToc = this.listAllLocationParameterConfig();
            for (Toc toc : allToc) {
                parameterLocation.add(toc.getLocationCode());
            }
            CacheKit.put("toc", "parameterLocation",parameterLocation);
        }

        return parameterLocation;
    }

    /**
     * 列出所有产线级参数
     * @return
     */
    public List<Toc> listAllLineParameterConfig(){
        List<Toc> lineParameterConfig = CacheKit.get("toc", "listAllLineParameterConfig");
        if(lineParameterConfig==null) {
            List<Toc> allToc = this.listAllToc();
            lineParameterConfig = Lists.newArrayList();
            for (Toc toc : allToc) {
                if (toc.getParameterType().intValue() == 2) {
                    lineParameterConfig.add(toc);
                }
            }
            CacheKit.put("toc", "listAllLineParameterConfig",lineParameterConfig);
        }
        return lineParameterConfig;
    }

    /**
     * 列出所有厂级参数
     * @return
     */
    public List<Toc> listAllFactoryParameterConfig(){
        List<Toc> factoryParameterConfig = CacheKit.get("toc", "listAllFactoryParameterConfig");
        if(factoryParameterConfig==null) {
            List<Toc> allToc = this.listAllToc();
            factoryParameterConfig = Lists.newArrayList();
            for (Toc toc : allToc) {
                if (toc.getParameterType().intValue() == 3) {
                    factoryParameterConfig.add(toc);
                }
            }
            CacheKit.put("toc", "listAllFactoryParameterConfig",factoryParameterConfig);
        }
        return factoryParameterConfig;
    }

    /**
     * @Description:  参数配置页的列表信息
     * @param:  [offset, remark] remark为工序名
     * @return: com.jfinal.kit.Ret
     * @author : gkw
     */
    public Ret configPagenate(Integer offset,String remark){
        Integer pageNumber=offset/100+1;
        Kv condition =Kv.by("remark",remark);
        Page<Record> page=Db.template("log.configPagenate", condition).paginate(pageNumber, 100);
        return Ret.ok().set("rows",page.getList()).set("total",page.getTotalRow());
    }

    /**
     * @Description:   根据ID查找
     * @param:  [id]
     * @return: io.shallwe.common.model.Toc
     * @author : gkw
     */
    public Toc findById(Integer id){
        Kv condition =Kv.by("id",id);
        return dao.template("log.findById",condition).findFirst();
    }

    /**
     * @Description:   参数为有效
     * @param:  [id]
     * @return: com.jfinal.kit.Ret
     * @author : gkw
     */
    public Ret doControl(Integer id){
        int n = Db.update("update log_toc set control_flag = ? where id=?", Toc.do_control, id);
        if (n > 0) {
            return Ret.ok("msg", "成功");
        } else {
            return Ret.fail("msg", "失败");
        }

    }

    /**
     * @Description:  参数为无效
     * @param:  [id]
     * @return: com.jfinal.kit.Ret
     * @author : gkw
     */
    public Ret notControl(Integer id){
        int n = Db.update("update log_toc set control_flag = ? where id=?", Toc.not_control, id);
        if (n > 0) {
            return Ret.ok("msg", "成功");
        } else {
            return Ret.fail("msg", "失败");
        }
    }

    /**
     * @Description:  参数上下限修改
     * @param:  [id, maxValue, minValue]
     * @return: com.jfinal.kit.Ret
     * @author : gkw
     */
    public Ret update(Integer id,Float maxValue,Float minValue){
        Toc toc = findById(id);
        toc.setParameterMaxValue(maxValue);
        toc.setParameterMinValue(minValue);
        if(toc.update()){
            return Ret.ok("msg", "修改成功");
        }else {
            return Ret.fail("msg", "修改失败");
        }
    }


}
